package com.fosun.spr.plugin.uuc.api.uucMenu.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fosun.spr.plugin.uuc.api.uucMenu.dao.UucMenuDao;
import com.fosun.spr.plugin.uuc.api.uucMenu.service.UucMenuService;
import com.fosun.spr.plugin.uuc.api.uucMenu.service.model.CreateOrUpdateSpModel;
import com.fosun.spr.plugin.uuc.api.uucMenu.service.model.CreateOrUpdateSrModel;
import com.fosun.spr.plugin.uuc.api.uucMenu.service.model.DeleteSpModel;
import com.fosun.spr.plugin.uuc.api.uucMenu.service.model.DeleteSrModel;
import com.fosun.spr.plugin.uuc.api.uucMenu.service.model.GetSpModel;
import com.fosun.spr.plugin.uuc.api.uucMenu.service.model.GetSrModel;
import com.fosun.spr.plugin.uuc.api.uucMenu.service.model.ListSpModel;
import com.fosun.spr.plugin.uuc.api.uucMenu.service.model.ListSrModel;
import com.fosun.spr.plugin.uuc.helper.mybatis.dao.SprUucMenuMapper;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucMenu;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucMenu;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucMenuExample;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.exception.ServiceException;
import com.fosun.spr.core.utils.ExtAsserts;


@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class UucMenuServiceImpl extends CoBaseService implements
		UucMenuService {
	@Autowired 
	private UucMenuDao uucMenuDao;
	@Autowired
	private SprUucMenuMapper sprUucMenuMapper;

	@Override
	public ListSrModel list(ListSpModel spModel) {
		ListSrModel srModel = new ListSrModel();
		srModel.setDatas(uucMenuDao.list(spModel));
		srModel.setTotalCount(uucMenuDao.listCount(spModel));
		return srModel;
	}

	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		GetSrModel srModel = new GetSrModel();
		SprUucMenu db = sprUucMenuMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db,"菜单管理");
		cglibCopyProperties(db, srModel, true);	
		return srModel;
	}
	
	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		DeleteSrModel srModel = new DeleteSrModel();
		SprUucMenu db = sprUucMenuMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db);
		db.setIsDeleted(IS_DELETED_Y);
		db.setRecUpdateTime(new Date());
		sprUucMenuMapper.updateByPrimaryKey(db);
		return srModel;
	}
	
	@Override
	public CreateOrUpdateSrModel createOrUpdate(CreateOrUpdateSpModel spModel) {
		ExtAsserts.notNull(spModel.getMenuCode(),"菜单代码");
		ExtAsserts.notNull(spModel.getMenuName(),"菜单名称");
		ExtAsserts.notNull(spModel.getNo(),"排序");
		
		CreateOrUpdateSrModel srModel = new CreateOrUpdateSrModel();
		//唯一性校验
		SprUucMenuExample eMenuCode = new SprUucMenuExample();
		SprUucMenuExample.Criteria cMenuCode = eMenuCode.createCriteria();
		cMenuCode.andMenuCodeEqualTo(spModel.getMenuCode());
		cMenuCode.andIsDeletedEqualTo(IS_DELETED_N);
		List<SprUucMenu> rowsMenuCode = sprUucMenuMapper.selectByExample(eMenuCode);		
		
		if(StringUtils.isBlank(spModel.getId())){
			if(rowsMenuCode.size()>0) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getMenuCode()+" 已经存在");
			}		
			
			SprUucMenu db = new SprUucMenu();
			cglibCopyProperties(spModel, db, true);
			db.setIsDeleted(IS_DELETED_N);
			db.setRecCreateTime(new Date());
			db.setId(getUuid());
			sprUucMenuMapper.insert(db);
			srModel.setId(db.getId());
		}else{
			if(rowsMenuCode.size()>1) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getMenuCode()+" 已经存在");
			}			
			if(rowsMenuCode.size()==1 && !rowsMenuCode.get(0).getMenuCode().equals(spModel.getMenuCode())) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getMenuCode()+" 已经存在");
			}	
			
			SprUucMenu db = sprUucMenuMapper.selectByPrimaryKey(spModel.getId());
			db.setMenuCode(spModel.getMenuCode());
			db.setMenuName(spModel.getMenuName());
			db.setParentId(spModel.getParentId());
			db.setUrl(spModel.getUrl());
			db.setPrivilegeId(spModel.getPrivilegeId());
			db.setNo(spModel.getNo());
			db.setMemo(spModel.getMemo());
			sprUucMenuMapper.updateByPrimaryKey(db);
		}
		return srModel;
	}	
}
